var Scrollbar_Needed = false;

function getNewsSubcat(sender, eventArgs) {
  getNews(sender, parseInt(sender.name.substr(6)));
}

//Funktion zum Abrufen eines Feeds und einfgen der Eintrge in XAML
function getNews(s, category) {

  var url;
  clearNews(s);
  s = s.getHost().content.Root;
  buildSubcats(s, Math.floor(category/10)*10);
  //s.findName("subcat"+category).FontWeight = "Bold";
  //s.findName("subcat"+category).FontSize = "10";
  s.findName("subcat"+category).Foreground = "#FFFFFFFF"
  //s.findName("subcat"+category).FontStyle = "Italic";
  switch(category)
  {
    //Nachrichten
    case 10: url = "http://news.de.msn.com/rss_newshome.aspx";break;
    case 11: url = "http://news.de.msn.com/rss_newsheadlines_panorama.aspx";break;
    case 12: url = "http://news.de.msn.com/rss_newsheadlines_politik.aspx";break;
    case 13: url = "http://news.de.msn.com/rss_newsheadlines_wirtschaft.aspx";break;
    case 14: url = "http://partner.de.msn.com/msntoolbar/video-news.aspx";break;
    case 15: url = "http://news.de.msn.com/rss_news2weather1.aspx";break;
    
    /*-----------------------------------------------------------------*/
    
    //Stars & VIP    
    case 20: url = "http://unterhaltung.de.msn.com/rss/rss_star_news.aspx";break;
    case 21: url = "http://unterhaltung.de.msn.com/msntoolbar/starzitate.aspx";break;
    case 22: url = "http://unterhaltung.de.msn.com/rss/rss_musik_news.aspx";break;
    case 23: url = "http://unterhaltung.de.msn.com/msntoolbar/talk-of-fame.aspx";break;
    case 24: url = "http://partner.de.msn.com/msntoolbar/video-unterhaltung.aspx";break;
    
    /*--------------------------------------------------------------------*/
    
    //Lifestyle    
    case 30: url = "http://lifestyle.de.msn.com/msntoolbar/gesundheit.aspx";break;
    case 31: url = "http://beauty.lifestyle.msn.de/beauty/rss";break;
    case 32: url = "http://lifestyle.de.msn.com/msntoolbar/leben.aspx";break;
    case 33: url = "http://lifestyle.de.msn.com/msntoolbar/Liebe-und-Dating.aspx";break;
    case 34: url = "http://mode.lifestyle.msn.de/mode/rss";break;
    case 35: url = "http://partner.de.msn.com/msntoolbar/video-lifestyle.aspx";break;
    
    /*----------------------------------------------------------------------*/
    
    //Auto
    case 40: url = "http://auto.de.msn.com/rss_autonews.aspx";break;
    case 41: url = "http://auto.de.msn.com/rss_autotestberichte.aspx";break;
    case 42: url = "http://partner.de.msn.com/msntoolbar/video-auto.aspx";break;
    
    /*-----------------------------------------------------------------------*/
    
    //Computer
    case 50: url = "http://tech.de.msn.com/msntoolbar/sicherheit.aspx";break;
    case 51: url = "http://tech.de.msn.com/msntoolbar/downloads.aspx";break;
    case 52: url = "http://tech.de.msn.com/msntoolbar/kaufberatung.aspx";break;
    case 53: url = "http://tech.de.msn.com/msntoolbar/ratgeber.aspx";break;
    case 54: url = "http://tech.de.msn.com/msntoolbar/spiele.aspx";break;
    case 55: url = "http://partner.de.msn.com/msntoolbar/video-computer-technik.aspx";break;
    
    //Fuball
    case 60: url = "http://sport.msn.de/rss/news/fussball";break;
    case 61: url = "http://sport.msn.de/rss/news/bundesliga";break;
    case 62: url = "http://sport.msn.de/rss/news/2bundesliga";break;
    case 63: url = "http://sport.msn.de/rss/news/regionalliga";break;
    case 64: url = "http://sport.msn.de/rss/news/dfbpokal";break;
    case 65: url = "http://sport.msn.de/rss/news/nationalelf";break;
    case 66: url = "http://sport.msn.de/rss/news/intfussball";break;
    case 67: url = "http://sport.msn.de/rss/news/clnews";break;    
    case 68: url = "http://sport.msn.de/rss/news/uefacup";break;
    case 69: url = "http://sport.msn.de/rss/news/wm";break;
    case 610: url = "http://sport.msn.de/rss/news/em";break;
    case 611: url = "http://sport.msn.de/rss/news/oberliga";break;
    case 612: url = "http://sport.msn.de/rss/news/frauenfussball";break;
    case 613: url = "http://sport.msn.de/rss/news/jugendfussball";break;
    
    /*------------------------------------------------------------------*/
    
    //Sport
    case 70: url = "http://sport.de.msn.com/msntoolbar/formel1.aspx";break;
    case 71: url = "http://sport.de.msn.com/msntoolbar/DTM.aspx";break;
    case 72: url = "http://sport.msn.de/rss/news/basketball";break;
    case 73: url = "http://sport.msn.de/rss/news/eishockey";break;
    case 74: url = "http://sport.msn.de/rss/news/handball";break;
    case 75: url = "http://sport.msn.de/rss/news/radsport";break;
    case 76: url = "http://sport.msn.de/rss/news/tennis";break;
    case 77: url = "http://sport.de.msn.com/msntoolbar/motorrad.aspx";break;
    case 78: url = "http://sport.de.msn.com/msntoolbar/rallye.aspx";break;
    case 79: url = "http://sport.msn.de/rss/news/mehrsport";break;
    case 710: url = "http://partner.de.msn.com/msntoolbar/video-sport.aspx";break;
    
    /*------------------------------------------------------------------*/
    
    //Money
    case 80: url = "http://money.de.msn.com/rss_topthemen.aspx";break;
    case 81: url = "http://money.de.msn.com/rss_aktien.aspx";break;
    case 82: url = "http://money.de.msn.com/rss_immobilien.aspx";break;
    case 83: url = "http://money.de.msn.com/msntoolbar/karriere.aspx";break;    
    case 84: url = "http://partner.de.msn.com/msntoolbar/video-money.aspx";break;
    
    /*---------------------------------------------------------------------*/
    
    //Video
    case 90: url = "http://partner.de.msn.com/msntoolbar/video-latest.aspx";break;
    case 91: url = "http://partner.de.msn.com/msntoolbar/video-unterhaltung.aspx";break;
    case 92: url = "http://partner.de.msn.com/msntoolbar/video-user.aspx";break;
    
    /*---------------------------------------------------------------------*/
    
    //Suchtrends
    case 100: url = "http://unterhaltung.de.msn.com/suchtrends/msntoolbar.aspx";break;
    case 101: url = "http://unterhaltung.de.msn.com/msntoolbar/peoplesearch.aspx";break;
    case 102: url = "";break;
    
    /*----------------------------------------------------------------------*/
    
    //Shopping
    case 110: url = "http://shopping.msn.de/xml/xmlresults/shp/?bCatID=66&format=rss";break;
    case 111: url = "http://shopping.msn.de/xml/xmlresults/shp/?bCatID=65&format=rss";break;
    case 112: url = "http://shopping.msn.de/xml/xmlresults/shp/?bCatID=68&format=rss";break;
    case 113: url = "http://shopping.msn.de/xml/xmlresults/shp/?bCatID=972&format=rss";break;
    case 114: url = "http://shopping.msn.de/xml/xmlresults/shp/?bCatID=262&format=rss";break;
    case 115: url = "http://shopping.msn.de/xml/xmlresults/shp/?bCatID=11&format=rss";break;
    case 116: url = "http://shopping.msn.de/xml/xmlresults/shp/?bCatID=8&format=rss";break;
    case 117: url = "http://shopping.msn.de/xml/xmlresults/shp/?bCatID=2364&format=rss";break;
  }
  
  //prefs.set('newsID', category);         
  _IG_FetchXmlContent(url, function(response) {populateSilverlightNews(s,response);});

}

//Funktion zum Einfgen der einzelnen Eintrge in das Dokument
function populateSilverlightNews(s, response) {

    rssCon=s.findName("NewsItems");
    
    if(response == null || typeof(response) != "object" || 
                      response.firstChild == null || response.getElementsByTagName("item").length == 0) {
            
             err_obj = '<TextBlock Name="ErrorMsg" Canvas.Left="50" Canvas.Top="50" Width="300" Text="" FontSize="9" Foreground="#FF1F2146" TextWrapping="Wrap" />';
             err_xaml = s.getHost().content.createFromXaml(err_obj);
 
             s.findName("NewsItems").children.add(err_xaml);
             s.findName("ErrorMsg").Text = unescape("Dieser Kanal ist im Moment leider nicht erreichbar. Bitte versuchen Sie es sp%E4ter erneut.");
             
             fadeNewsIn(s);
             updateCanvas(s);
             
              return;
           }
    
    var entry;
    var canv_top = 20;
    
    var entries = response.getElementsByTagName("item");
    
    for(i=0; i < entries.length;i++) {
    entry = entries.item(i);
    
    
    var desc_val = "";    
    var title_val = entry.getElementsByTagName("title").item(0).firstChild.nodeValue;
    
    if(entry.getElementsByTagName("description").length != 0 && entry.getElementsByTagName("description").item(0).firstChild != null) {
      desc_val = deleteTags(entry.getElementsByTagName("description").item(0).firstChild.nodeValue);
    }
    
    s.findName("NewsItems").Opacity = "0";
    
    //Sonderzeichen maskieren
    re=/&/g;
    title_val=title_val.replace(re,"&amp;");
    desc_val=desc_val.replace(re,"&amp;");
    
    re=/\"/g;
    title_val=title_val.replace(re,"'");
    desc_val=desc_val.replace(re,"'");
    
    //Beim ersten Eintrag unter umstnden Bild laden
    if(i == 0) {
      if(entry.getElementsByTagName("enclosure").length != 0) {
        var img_str = '<Image Canvas.ZIndex="2" Name="news_img" Width="100" Canvas.Left="4" Canvas.Top="25" Source="';
        img_str += entry.getElementsByTagName("enclosure").item(0).getAttribute("url") + '" />';
        imgTags = s.getHost().content.createFromXaml(img_str);
        imgTags.Cursor="Hand";
        s.findName("NewsItemsContainer").children.add(imgTags);
        imgTags.addEventListener("MouseLeftButtonDown", handleMouseFeedDown);
        updateCanvas(s);
      }
    }
    
    //XAML-String erstellen
    var news_story_str = '<TextBlock Name="News'+i+'" Canvas.Top="'+canv_top+'" Text="'+title_val+'" TextWrapping="Wrap" FontSize="9" Foreground="#FF1F2146"';
    if(i == 0)news_story_str += ' FontWeight="Bold"';
    news_story_str += ' Cursor="Hand" />';
    
    var previewWidth = s.findName("NewsItems").ActualWidth;
    var previewLeft = 0;
    if(s.findName("news_img") != null)previewLeft = -30;
    if(previewLeft == -30)previewWidth += 25;
    
    var news_story_preview = '<Canvas Canvas.ZIndex="2" Canvas.Left="'+previewLeft+'" Width="'+previewWidth+'" Height="100" Name="Preview'+i+'" Background="#FFFEF592" Opacity="0" Visibility="Collapsed">';
    news_story_preview += '<TextBlock Width="'+previewWidth+'" Height="100" Name="PreviewText'+i+'" Text="'+desc_val+'" TextWrapping="Wrap" ';
    news_story_preview += 'FontSize="9" Foreground="#FF1F2146" Cursor="Arrow" />';
    re=/&/g;
    var link_val = entry.getElementsByTagName("link").item(0).firstChild.nodeValue;
    link_val=link_val.replace(re,"&amp;");
    news_story_preview += '<TextBlock Name="Link'+i+'" Text="'+link_val+'" Opacity="0" />';
    news_story_preview += '</Canvas>';
    
    canv_top += 17;
     
    //XAML anhngen
    var control = s.getHost();
    storyTags = control.content.createFromXaml(news_story_str);
    storyPreview = control.content.createFromXaml(news_story_preview);
    storyTags = trim(s, storyTags);
    
    //berprfen, ob die einzelnen Items noch in die Anzeige passen,
    //falls nicht ScrollBar anzeigen
    if((storyTags["Canvas.Top"] + 20) < s.findName("NewsItems").Height) {
      rssCon.children.add(storyTags);
      rssCon.children.add(storyPreview);
      storyTags.addEventListener("MouseEnter", handleMouseIn_News);
      storyTags.addEventListener("MouseLeave", handleMouseOut_News);
      storyTags.addEventListener("MouseLeftButtonDown", handleMouseFeedDown);
      s.findName("Preview"+i)["height"] = s.findName("PreviewText"+i).ActualHeight;
    }
    else{Scrollbar_Needed = true};
  }
  updateCanvas(s);
  fadeNewsIn(s);
}

function deleteTags(txt) {
  var txt_strip = txt;
  while(txt_strip.indexOf('<') != -1) {
    var first = txt.indexOf('<');
    var last = txt.indexOf('>');
    txt_strip = txt.substr(0, first);
    txt_strip = txt_strip + txt.substr(last+1);
    txt = txt_strip;
    
  }
  return txt_strip;
}

function stripWhiteSpace(node) {

  nodesToDelete = Array();
  findWhiteSpace(node, 0);
  
  for(i=nodesToDelete.length-1;i>=0;i--) {
    nodeRef = nodesToDelete[i];
    nodeRef.parentNode.removeChild(nodeRef);
  }
}

//Alle momentanten Eintrge lschen
function clearNews(s) {
  var canvas = s.findName("NewsItems");
  canvas.Children.Clear();
  if(s.findName("news_img") != null) {
    s.findName("NewsItemsContainer").Children.Remove(s.findName("news_img"));
  }
}

//Die Eintrge auf die richtige Lnge krzen
function trim(s, obj) {
  var can_width = Math.round(s.findName("NewsItems").ActualWidth) - Math.round(obj["Canvas.Left"]);
  while(obj.ActualWidth > can_width) {
    obj.Text = obj.Text.substr(0,obj.Text.lastIndexOf(' ')) + "...";
  }
  return obj;
}

//Feglermeldung anzeigen
function xaml_error(sender, errorArgs)
{
    {
    // The error message to display.
    var errorMsg = "Silverlight Error: \n\n";
    
    // Error information common to all errors.
    errorMsg += "Error Type:    " + errorArgs.errorType + "\n";
    errorMsg += "Error Message: " + errorArgs.errorMessage + "\n";
    errorMsg += "Error Code:    " + errorArgs.errorCode + "\n";
    
    // Determine the type of error and add specific error information.
    switch(errorArgs.errorType)
    {
        case "RuntimeError":
            // Display properties specific to RuntimeErrorEventArgs.
            if (errorArgs.lineNumber != 0) {
              errorMsg += "Line: " + errorArgs.lineNumber + "\n";
              errorMsg += "Position: " +  errorArgs.charPosition + "\n";
            }
            errorMsg += "MethodName: " + errorArgs.methodName + "\n";
            break;
        case "ParserError":
            // Display properties specific to ParserErrorEventArgs.
            errorMsg += "Xaml File:      " + errorArgs.xamlFile      + "\n";
            errorMsg += "Xml Element:    " + errorArgs.xmlElement    + "\n";
            errorMsg += "Xml Attribute:  " + errorArgs.xmlAttribute  + "\n";
            errorMsg += "Line:           " + errorArgs.lineNumber    + "\n";
            errorMsg += "Position:       " + errorArgs.charPosition  + "\n";
            break;
        default:
            break;
    }
    // Display the error message.
    alert(errorMsg);
}

}

